package com.etc;

import org.apache.commons.io.FileUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;

import java.io.File;
import java.io.IOException;

/**
 * A:B,C,D,F,E,O
 * B:A,C,E,K
 * C:F,A,D,I
 * D:A,E,F,L
 * E:B,C,D,M,L
 * F:A,B,C,D,E,O,M
 * G:A,C,D,E,F
 * H:A,C,D,E,O
 * I:A,O
 * J:B,O
 * K:A,C,D
 * L:D,E,F
 * M:E,F,G
 * O:A,H,I,J
 */
public class Friend {
    public static class FriendMap extends Mapper<LongWritable, Text,Text,Text> {

        Text k = new Text();//朋友
        Text v = new Text();//人

        @Override
        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
            String line = value.toString();
            //先用":"把人和朋友分开  {"A" , "B,C,D,F,E,O"}
            String[] Array = line.split(":");
            //根据人的下标获取到人
            String person = Array[0];
            //根据朋友的的下标获取到所有朋友,再用","把朋友分开 {"B","C","D","F","E","O"}
            String[] friends = Array[1].split(",");
            //把朋友作为key  人作为value传输给reduce(B,A)(C,A)(D,A)(F,A)(E,A)(O,A)
            for (String friend : friends) {
                k.set(friend);
                v.set(person);
                context.write(k, v);
            }
        }
    }
    public static class FriendReduce extends Reducer<Text,Text,Text,Text> {
        @Override
        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
            StringBuffer str = new StringBuffer();
            //new 一个 StringBuffer在每个朋友后面追加","
            for (Text value : values) {
                //根据key聚合,输出形式例如  A	 I,K,C,B,G,F,H,O,D,  找出所有有A好友的人，即谁的好友是A
                str.append(value.toString()).append(",");
            }
            //得到
            context.write(key,new Text(str.toString()));

        }

    }
    public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
        Configuration conf = new Configuration();
        //创建一个mapreduce的工作
        Job job = Job.getInstance(conf);
        job.setJarByClass(Friend.class);
        //map的class在哪里
        job.setMapperClass(Friend.FriendMap.class);
        //reduce的class在哪里
        job.setReducerClass(Friend.FriendReduce.class);
        //map输出的key和value的类型
        job.setMapOutputKeyClass(Text.class);
        job.setMapOutputValueClass(Text.class);
        //reduce输出的key和value的类型
        job.setOutputKeyClass(Text.class);
        job.setOutputValueClass(Text.class);

        File file = new File("F:\\output");
        if (file.exists()) {
            FileUtils.deleteDirectory(file);
        }
        //计算的文件在哪里
        FileInputFormat.setInputPaths(job, new Path("F:\\friend"));
        //计算的结果在哪里
        FileOutputFormat.setOutputPath(job, new Path("F:\\output"));

        job.waitForCompletion(true);
    }
}
